/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.ice.tar.TarEntry;
import com.ice.tar.TarInputStream;
import java.io.OutputStream;
import org.apache.log4j.Logger;

/**
 *
 * @author Austin
 */
public class TarTools {

    public static void main(String[] args) {
        TarTools ifm = new TarTools();
        ifm.inflat("C:\\Users\\Austin\\Desktop\\桌面\\烽火wx数据", "test.tar", "C:\\Users\\Austin\\Desktop\\桌面\\烽火wx数据");
    }

    private static Logger logger = Logger.getLogger(TarTools.class);

    public boolean inflat(String fileDir, String inFileName, String decompressionDir) {
        boolean flag = false;
        long startTime = System.currentTimeMillis();

        InputStream inputstream = null;
        OutputStream outputstream = null;
        TarInputStream zis = null;
        String str = null;
        try {
            logger.info("解压文件： " + fileDir + inFileName);
            File file = new File(fileDir + File.separator + inFileName);
            File tempFile = null;
            inputstream = new FileInputStream(file);
            zis = new TarInputStream(inputstream);
            TarEntry tarEntry = null;//关键在于这个TarEntry 的理解，实际你的tar包里有多少文件就有多少TarEntry
            String newFileName = "";
            for (int i = 0; i < inFileName.split("\\.").length - 1; i++) {
                newFileName = newFileName + inFileName.split("\\.")[i];
            }
            while ((tarEntry = zis.getNextEntry()) != null) {
//                File newFile = new File(decompressionDir + newFileName);
//                //判断文件夹是否存在 
//                if (!newFile.exists()) {
//                    newFile.mkdirs();
//                }
                tempFile = new File(decompressionDir + File.separator + tarEntry.getName());
                logger.info("解压文件至" + tempFile);
                tempFile.createNewFile();
                outputstream = new FileOutputStream(tempFile);
                //定一个缓存池 可以根据实际情况调整大小（事实证明很有用） 
                byte[] buffer = new byte[1024 * 50];
                while (true) {
                    int readsize = zis.read(buffer);
                    outputstream.write(buffer);
                    if (readsize < 1024 * 50) {
                        break;
                    }
                }
            }
            logger.info(fileDir + File.separator + inFileName + "解压成功");

            logger.info("Closing the file and stream");
            outputstream.flush();
            inputstream.close();
            zis.close();
            outputstream.close();

            flag = true;
        } catch (Exception e) {
            logger.info("解压文件" + fileDir + File.separator + inFileName + "失败： " + e);
        } finally {

        }
        long endTime = System.currentTimeMillis();
        logger.info("解压tar压缩包开始时间： " + startTime + " ，结束时间：" + endTime + " ，消耗时间： " + (endTime - startTime) + " ms");
        return flag;
    }

    public static String getFileName(String fileName) {
        String fname = "";
        int i = fileName.lastIndexOf('.');

        if (i > 0 && i < fileName.length() - 1) {
            fname = fileName.substring(0, i);
        }
        return fname;
    }

}
